#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    iSANClient = new SANClient();
    iSANReceive = new  SANReceive();
    connect(iSANClient, SIGNAL(EmitClientConnected(bool)), this, SLOT(ClientConnected(bool)));
    connect(iSANReceive, SIGNAL(EmitClientAccepted(bool)), this, SLOT(ClientAccepted(bool)));
    connect(iSANReceive, SIGNAL(EmitServerStart(bool)), this, SLOT(ServerStart(bool)));
    connect(iSANClient, SIGNAL(EmitDataSent(char*)), this, SLOT(DataSent(char*)));
    connect(iSANReceive, SIGNAL(EmitDisplaySortedData(QString)), this, SLOT(DisplaySortedData(QString)));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_pressed()
{
    bool status;
    QString name    = ui->lineEdit->text();
    std::string name_1   = name.toStdString();
    QString ip      = ui->lineEdit_2->text();
    std::string ip_1     = ip.toStdString();
    QString port    = ui->lineEdit_3->text();
    int port_1      = port.toInt();

    name_1 = "big.txt";
    //ip_1 = "127.0.0.1";
    ip_1 = "192.168.110.201";
    port_1 = 3600;
    //port_1 = 9993;

    iSANClient->SetDestnIp(ip_1);
    iSANClient->SetDestnPort(port_1);
    status = iSANClient->OpenFile(name_1);
    status = iSANClient->SetupConnection() && status;
    status = iSANClient->ConnectToServer() && status;
    if(status == true)
    {
        QMessageBox temp;
        int ret;
        temp.setText("Client connected");
        temp.setStandardButtons(QMessageBox::Ok);
        ret = temp.exec();
        switch(ret)
        {
            case QMessageBox::Ok:
                   break;
            default:
                break;
        }
        ui->page->hide();
        ui->page_2->show();
    }
    else
    {
        QMessageBox temp;
        int ret;
        temp.setText("Client not connected");
        temp.setStandardButtons(QMessageBox::Ok);
        ret = temp.exec();
        switch(ret)
        {
            case QMessageBox::Ok:
                exit(0);
                break;
            default:
                break;
        }
    }
}

void MainWindow::on_pushButton_2_pressed()
{
    if(iSANClient->isRunning() == true)
        iSANClient->quit();
    if(iSANReceive->isRunning() == true)
        iSANReceive->quit();

    delete iSANClient;
    delete iSANReceive;
    exit(0);
}

void MainWindow::on_pushButton_3_pressed()
{
    if(iSANClient->isRunning() == true)
        iSANClient->quit();
    if(iSANReceive->isRunning() == true)
        iSANReceive->quit();

    delete iSANClient;
    delete iSANReceive;
    exit(0);
}

void MainWindow::on_pushButton_4_pressed()
{
    if(ui->radioButton->isChecked() == true)
    {
        iSANClient->SetDistribution(KPoisson);
    }
    else if(ui->radioButton_2->isChecked() == true)
    {
        iSANClient->SetDistribution(KBinomial);
    }
    else
        iSANClient->SetDistribution(KUniform);


    QString rate = ui->lineEdit_4->text();
    double rate_1 = rate.toDouble();
    qDebug("rate : %f", rate_1);
    if(rate == 0)
        iSANClient->SetSendRate(1);
    else
        iSANClient->SetSendRate(rate_1);

    if(iSANClient->GetDistribution() != KPoisson)
    {
        double delay = (DATA_LEN )/(rate_1 * 1000000);
        qDebug("delay %f", delay);

        //in secs
        iSANClient->iUSleep = delay * 1000000;
    }
    qDebug("sleep time %d",iSANClient->iUSleep);
    qDebug("distribution is %d", iSANClient->GetDistribution());
    ui->page_2->hide();
    ui->page_4->show();

}

void MainWindow::on_pushButton_5_pressed()
{
    if(iSANClient->isRunning() == true)
    {
        iSANClient->terminate();
        iSANClient->quit();
    }
    if(iSANReceive->isRunning() == true)
    {
        iSANReceive->terminate();
        iSANReceive->quit();
    }
    delete iSANClient;
    delete iSANReceive;
    exit(0);
}

void MainWindow::on_pushButton_6_pressed()
{
    iSANClient->start();
    iSANReceive->start();
}

void MainWindow::ClientConnected(bool aStatus)
{
    QMessageBox temp;
    int ret;
    if(aStatus == true)
        temp.setText("Client Connected");
    else
        temp.setText("Client not connected");

    temp.setStandardButtons(QMessageBox::Ok);
    ret = temp.exec();

    switch(ret)
    {
        case QMessageBox::Ok:
            ui->page->hide();
            ui->page_2->show();
            break;
        default:
            break;
    }
}


void MainWindow::on_pushButton_7_pressed()
{
    if(iSANClient->isRunning() == true)
        iSANClient->terminate();

    if(iSANReceive->isRunning() == true)
        iSANReceive->terminate();

    long long count = iSANClient->GetCount();
    long long count_1 = iSANReceive->GetCount();

    QString num;
    num = QString::number(count);
    num.append(" packets sent! ");

    QString num_1;
    num_1 = QString::number(count_1);
    num_1.append(" packets received!");

    QString final = num + num_1;
    QByteArray w = final.toUtf8();
    char *t = w.data();
    qDebug("Result %s", t);
    qDebug("Totaldelay = %f", iSANClient->GetTotDelay());
    qDebug("Average delay = %f", iSANClient->GetTotDelay()/(double)count);

}

void MainWindow::on_pushButton_9_pressed()
{
    if(iSANClient->isRunning() == true)
        iSANClient->quit();
    if(iSANReceive->isRunning() == true)
        iSANReceive->quit();
    delete iSANClient;
    delete iSANReceive;
    exit(0);
}

void MainWindow::on_pushButton_8_pressed()
{

    QString port = ui->lineEdit_6->text();
    int port_1 = port.toInt();

    iSANReceive->SetPort(port_1);
    iSANReceive->start();

}

void MainWindow::ClientAccepted(bool aStatus)
{
    QMessageBox temp;
    int ret;
    if(aStatus == true)
        temp.setText("Server accepted as client");
    else
        temp.setText("Server not accepted as client");

    temp.setStandardButtons(QMessageBox::Ok);
    ret = temp.exec();

    switch(ret)
    {
        case QMessageBox::Ok:
              break;
        default:
            break;
    }

}

void MainWindow::ServerStart(bool aStatus)
{
    QMessageBox temp;
    int ret;
    if(aStatus == true)
        temp.setText("Server started at client");
    else
        temp.setText("Server not started at client");

    temp.setStandardButtons(QMessageBox::Ok);
    ret = temp.exec();

    switch(ret)
    {
        case QMessageBox::Ok:
            ui->page_4->hide();
            ui->page_3->show();
            break;
        default:
            break;
    }
}

void MainWindow::DataSent(char *aBuf)
{
    QString temp(aBuf);
    ui->listWidget->addItem(temp);
    ui->listWidget->scrollToBottom();
}

void MainWindow::DisplaySortedData(QString temp)
{

    ui->listWidget_2->addItem(temp);
    ui->listWidget_2->scrollToBottom();
}
